//package com.dongzili.easysearch.common.config;
//
//import org.apache.http.HttpHost;
//import org.apache.http.auth.AuthScope;
//import org.apache.http.auth.UsernamePasswordCredentials;
//import org.apache.http.client.CredentialsProvider;
//import org.apache.http.conn.ssl.NoopHostnameVerifier;
//import org.apache.http.impl.client.BasicCredentialsProvider;
//import org.apache.http.ssl.SSLContexts;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestClientBuilder;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
//
//import javax.net.ssl.SSLContext;
//import java.security.KeyManagementException;
//import java.security.KeyStoreException;
//import java.security.NoSuchAlgorithmException;
//
//@Configuration
//public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
//    @Value("${spring.elasticsearch.uris}")
//    private String elasticsearchUrl;
//
//    @Value("${spring.elasticsearch.username}")
//    private String username;
//
//    @Value("${spring.elasticsearch.password}")
//    private String password;
//
//    @Override
//    @Bean
//    public RestHighLevelClient elasticsearchClient() {
//        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY,
//                new UsernamePasswordCredentials(username, password));
//
//        try{
//            SSLContext sslContext = SSLContexts.custom()
//                    .loadTrustMaterial(null, (x509Certificates, s) -> true)
//                    .build();
//
//            RestClientBuilder builder = RestClient.builder(HttpHost.create(elasticsearchUrl))
//                    .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
//                            .setDefaultCredentialsProvider(credentialsProvider)
//                            .setSSLContext(sslContext)
//                            .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE));
//            return new RestHighLevelClient(builder);
//        } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
//            throw new RuntimeException(e);
//        }
//    }
//}